package com.wdd.myplatform.utils;

import cn.hutool.core.date.DatePattern;
import com.google.common.collect.Range;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;

/**
 * @author Administrator
 */
@Slf4j
public class TableRangeShardingAlgorithm implements RangeShardingAlgorithm<String> {

    @SneakyThrows
    @Override
    public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<String> rangeShardingValue) {
        String tbName = rangeShardingValue.getLogicTableName();
        Range<String> range = rangeShardingValue.getValueRange();
        Date startDate = null;
        Date endDate = null;
        Calendar calendar = Calendar.getInstance();
        if (range == null) {
            calendar.add(Calendar.MONTH, -1);
            startDate = calendar.getTime();
            endDate = new Date();
        } else {
            if (range.hasLowerBound()) {
                String s = range.lowerEndpoint();
                startDate = DateUtils.parseDate(s, DatePattern.NORM_DATETIME_PATTERN);
//                startDate = range.lowerEndpoint();
            } else {
                calendar.add(Calendar.MONTH, -1);
                startDate = calendar.getTime();
            }
            if (range.hasUpperBound()) {
//                endDate = range.upperEndpoint();
                endDate = DateUtils.parseDate(range.upperEndpoint(), DatePattern.NORM_DATETIME_PATTERN);
            } else {
                endDate = new Date();
            }
        }
        Calendar calStart = Calendar.getInstance();
        calStart.setTime(startDate);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(endDate);
        HashSet<String> result = new HashSet<>();
        int endYear = calEnd.get(Calendar.YEAR);
        int endMonth = calEnd.get(Calendar.MONTH) + 1;
        int endYM = endYear * 12 + endMonth;
        int startYear = calStart.get(Calendar.YEAR);
        int startMonth = calStart.get(Calendar.MONTH);
        int startYM = startYear * 12 + startMonth;
        while (startYM <= endYM) {
            log.debug("startYM:{},endYM:{}", startYM, endYM);
            int year = calStart.get(Calendar.YEAR);
            int month = calStart.get(Calendar.MONTH) + 1;
            String tableName = tbName + year;
            if (month < 10) {
                tableName = tableName + "0" + month;
            } else {
                tableName = tableName + month;
            }
            log.debug("startMonth:{},endMonth:{},tableName:{}", startMonth, endMonth, tableName);
            result.add(tableName);
            calStart.add(Calendar.MONTH, 1);
            startYM = calStart.get(Calendar.YEAR) * 12 + calStart.get(Calendar.MONTH) + 1;
        }
        return result;
    }


}
